package end.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import end.bean.Customer;
import end.ser.CustomerSER;
import end.util.DateUtils;
import end.util.jedis.JedisUtil;

/**
 * 
 * CustomerForController.java 2016-6-29
 * 
 * Copyright zhaocj Inc. All rights reserved. Love Me Like Love Justin Bieber
 */
@Controller
@RequestMapping("/customer/")
public class CustomerForController {
	@Autowired
	private CustomerSER<Customer> customerSER;

	public CustomerSER<Customer> getCustomerSER() {
		return customerSER;
	}

	public void setCustomerSER(CustomerSER<Customer> customerSER) {
		this.customerSER = customerSER;
	}

	@RequestMapping("queryCustomersByPage")
	public @ResponseBody
	Map<String, Object> queryCustomersByPage(Integer page, Integer rows,
			String sort, String order) throws Exception {
		Customer customer = new Customer();
		customer.getPageUtil().setPaging(true);
		customer.getPageUtil().setOrderByCondition(sort);
		customer.getPageUtil().setQueryCondition(order);
		customer.getPageUtil().setPageOffset((page - 1) * rows);
		customer.getPageUtil().setPageSize(rows);
		customer.setUseStatus('0');
		List<Customer> beans = customerSER.selectByBean(customer);
		Map<String, Object> rets = new HashMap<String, Object>();
		rets.put("total", customerSER.selectByBeanCount(customer));
		rets.put("rows", beans);
		return rets;
	}

	@RequestMapping("modify")
	public @ResponseBody
	boolean modifyCustomer(Customer customer, HttpSession session,
			MultipartFile avatarImg, String avatarChangeFlag) throws Exception {
		ServletContext context = session.getServletContext();
		String realPath = context.getRealPath("/static/file/customer/avatar");
		if (avatarChangeFlag.equals("ALCHANGE")) {
			if (!avatarImg.isEmpty()) {
				if (!customer.getAvatar().equals("NOIMAGE")) {
					FileUtils.forceDelete(new File(realPath + "/"
							+ customer.getAvatar()));
				}
				String suffix = avatarImg.getOriginalFilename().substring(
						avatarImg.getOriginalFilename().lastIndexOf("."));
				avatarImg.transferTo(new File(realPath + "/" + customer.getId()
						+ suffix));
				customer.setAvatar(customer.getId() + suffix);
			} else {
				// 如果没有上传图片 并且之前有图片的话 删除服务器的图片
				if (!customer.getAvatar().equals("NOIMAGE")) {
					FileUtils.forceDelete(new File(realPath + "/"
							+ customer.getAvatar()));
					customer.setAvatar("NOIMAGE");
				}
			}
		}
		customerSER.updateByPrimaryKey(customer);
		return true;
	}

	@RequestMapping("register")
	public @ResponseBody
	boolean insertCustomer(Customer customer, HttpSession session,
			MultipartFile avatarImg) throws Exception {
		ServletContext context = session.getServletContext();
		customer.setUseStatus('0');
		customer.setRegisterTime(new Date());
		customerSER.insert(customer);
		JedisUtil.lpushUsers(
				"last_reg_customers" + DateUtils.getDate("yyyy-MM-dd"),
				customer.getId());
		if (!new File(context.getRealPath("/static/file/customer/avatar"))
				.exists()) {
			FileUtils.forceMkdir(new File(context
					.getRealPath("/static/file/customer/avatar")));
		}
		String realPath = context.getRealPath("/static/file/customer/avatar");
		if (!avatarImg.isEmpty()) {
			String suffix = avatarImg.getOriginalFilename().substring(
					avatarImg.getOriginalFilename().lastIndexOf("."));
			avatarImg.transferTo(new File(realPath + "/" + customer.getId()
					+ suffix));
			customer.setAvatar(customer.getId() + suffix);
		} else {
			customer.setAvatar("NOIMAGE");
		}
		customerSER.updateByPrimaryKey(customer);
		return true;
	}

	@RequestMapping("drop")
	public @ResponseBody
	boolean dropCustomers(Integer[] drops) throws Exception {
		List<Integer> dropsArray = new ArrayList<Integer>();
		for (Integer integer : drops) {
			dropsArray.add(integer);
		}
		customerSER.deleteByPrimaryKey(dropsArray);
		return true;
	}
}
